package com.cci.kangdao.utilTool;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class DigestUtil {

    public static String digest(String accessSecret, String content) {
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            byte[] secretByte = accessSecret.getBytes("utf-8");
            byte[] dataBytes = content.getBytes("utf-8");

            SecretKey secret = new SecretKeySpec(secretByte, "HmacSHA1");
            mac.init(secret);

            byte[] doFinal = mac.doFinal(dataBytes);
            byte[] encodeBase64 = Base64.encodeBase64(doFinal);
            return new String(encodeBase64, "utf-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}